import asyncio
import time

from playwright.async_api import async_playwright


async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        page = await browser.new_page()
        # 修改默认等待超时时间
        await page.set_default_timeout(50000)
        await page.goto("www.baidu.com")
        #强制等待时间
        await page.wait_for_timeout(1000)

        # 等待页面处于某种状态
        await page.wait_for_load_state('load')

        # 等待元素出现
        await page.wait_for_selector('#su')
        #等待元素消失
        await page.wait_for_selector('#su','hidden')

        # 等待js函数执行完
        # 等待一个元素的文本内容变为 "成功"
        await page.wait_for_function(
            "document.querySelector('div#status').innerText === '成功'"
        )
        # 等待事件发生
        # 等待对话框弹出
        dialog = page.wait_for_event("dialog")
        # 等待页面加载完成
        await page.wait_for_event("load")

        await browser.close()

asyncio.run(main())
